<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    全选<input type="checkbox" name="" id="allSelect"><br>
    <div>
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
    </div>
    <script>
        var allSelect = document.getElementById('allSelect');
        var allCheckbox = document.querySelector('div').getElementsByTagName('input');
        allSelect.onclick = function () {
            for (let i in allCheckbox) {
                if (allSelect.checked) {
                    allCheckbox[i].checked = this.checked;
                } else {
                    allCheckbox[i].checked = this.checked;
                }
            }
        }
        for (let i in allCheckbox) {
            allCheckbox[i].onclick = function () {
                // flag 控制全局按钮是否被选中
                var flag = true;
                for (let i in allCheckbox) {
                    if (allCheckbox[i].checked == false) {
                        flag = false;
                        // console.log('false');
                        break;
                    }
                }
                allSelect.checked = flag;
            }
        }

        // 获取元素属性值
        console.log(allSelect.getAttribute('id'));
        // 设置元素属性值
        allSelect.setAttribute('name', 'allSel');
        console.log(allSelect.getAttribute('name'));
        // 移除属性
        allSelect.removeAttribute('name');

    </script>
</body>

</html>